package com.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.entity.Product;
import com.demo.service.ProductService;
import com.demo.common.vo.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * controller
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;

    /**
     * 新增
     * @param product
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Product product){
        productService.save(product);
        return Result.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id){
        productService.removeById(id);
        return Result.success();
    }

    /**
     * 更新
     * @param product
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Product product){
        if(product.getId()==null){
            return Result.error("更新记录主键不能为空");
        }
        productService.updateById(product);
        return Result.success();
    }

    /**
     * 列表
     * @param product
     * @return
     */
    @PostMapping("/list")
    public Result list(@RequestBody Product product){
        return Result.success(productService.list(new QueryWrapper<>(product)));
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result list(@RequestParam Integer id){
        return Result.success(productService.getById(id));
    }

}
